package com.ihrm.modules.sys.controller;

import java.util.Arrays;
import java.util.Map;


import com.ihrm.common.utils.IdWorker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ihrm.modules.sys.entity.CoDepartmentEntity;
import com.ihrm.modules.sys.service.CoDepartmentService;
import com.ihrm.common.utils.PageUtils;
import com.ihrm.common.utils.R;


/**
 * 部门管理
 *
 * @author gaogzhen
 * @email gaogzhen@foxmail.com
 * @date 2020-11-20 19:45:14
 */
@RestController
@RequestMapping("sys/codepartment")
@Api(tags = "部门管理")
public class CoDepartmentController {
    @Autowired
    private CoDepartmentService coDepartmentService;

    @Autowired
    IdWorker idWorker;

    /**
     * 列表
     */
    @GetMapping
    @ApiOperation("列表")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = coDepartmentService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询部门信息")
    public R info(@PathVariable("id") Long id) {
        CoDepartmentEntity coDepartment = coDepartmentService.getById(id);

        return R.ok().put("coDepartment", coDepartment);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation("添加")
    public R save(@RequestBody CoDepartmentEntity coDepartment) {
        coDepartmentService.save(coDepartment);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public R update(@RequestBody CoDepartmentEntity coDepartment) {
        // ValidatorUtils.validateEntity(coDepartment);
        coDepartmentService.updateById(coDepartment);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @ApiOperation("删除")
    public R delete(@RequestBody Long[] ids) {
        coDepartmentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据ID删除部门及其子部门
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除")
    public R deleteSub(@PathVariable String id) {
       coDepartmentService.deleteSub(id);

        return R.ok();
    }

}
